package onion.molingo2o.db;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.TextUtils;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import onion.molingo2o.R;


public class DBHelper extends SQLiteOpenHelper {

	private Context mContext;


	public DBHelper(Context context, String name, CursorFactory factory,
					int version) {
		super(context, name, factory, version);
		mContext = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {

		db.execSQL("CREATE TABLE province(_id INTEGER PRIMARY KEY AUTOINCREMENT,"
				+ "proindex VARCHAR(50),"
				+ "proname  VARCHAR(50))");

		db.execSQL("CREATE TABLE city(_id INTEGER PRIMARY KEY AUTOINCREMENT,"
				+ "proindex VARCHAR(50),"
				+ "cityindex VARCHAR(50),"
				+ "cityname  VARCHAR(50))");

		db.execSQL("CREATE TABLE town(_id INTEGER PRIMARY KEY AUTOINCREMENT,"
				+ "proindex VARCHAR(50),"
				+ "cityindex VARCHAR(50),"
				+ "townindex VARCHAR(50),"
				+ "townname  VARCHAR(50));");



		//		initCities3Table(db);
		initProvinceTable(db);
		initCityTable(db);
		initTownTable(db);




	}
	//------------鍖�-----------------------------
	private void initTownTable(SQLiteDatabase db) {

		InputStream cityIs = null;
		BufferedReader cityBuffer = null;
		try {
			cityIs = mContext.getResources().openRawResource(R.raw.town);
			cityBuffer = new BufferedReader(new InputStreamReader(cityIs));
			String cityline = null;
			while((cityline = cityBuffer.readLine()) != null){
				if(!TextUtils.isEmpty(cityline)){

					String[] strList = cityline.split("\t");
					if (strList.length < 4) {
						continue;
					}

					db.execSQL("INSERT INTO town(proindex,cityindex,townindex,townname) VALUES (?,?,?,?)",
							new Object[] {strList[3].trim(),strList[2].trim(),strList[0].trim(), strList[1].trim()
					});
				}
			}
		} catch (SQLException e) {
			Log.e("DD", "SQLException: " + e.getMessage());
		} catch (IOException e) {
			Log.e("DD", "SQLException: " + e.getMessage());
		} finally {
			if(cityBuffer != null){
				try {
					cityBuffer.close();
				} catch (IOException e) {
					Log.e("DD", "SQLException: " + e.getMessage());
				}
			}

			if(cityIs != null){
				try {
					cityIs.close();
				} catch (IOException e) {
					Log.e("DD", "SQLException: " + e.getMessage());
				}
			}
		}




	}
	//-----------------------------甯�----------------------
	private void initCityTable(SQLiteDatabase db) {


		InputStream cityIs = null;
		BufferedReader cityBuffer = null;
		try {
			cityIs = mContext.getResources().openRawResource(R.raw.city);
			cityBuffer = new BufferedReader(new InputStreamReader(cityIs));
			String cityline = null;
			while((cityline = cityBuffer.readLine()) != null){
				if(!TextUtils.isEmpty(cityline)){

					String[] strList = cityline.split("\t");
					if (strList.length < 3) {
						continue;
					}

					db.execSQL("INSERT INTO city(proindex,cityindex,cityname) VALUES (?,?,?)",
							new Object[] {strList[2].trim(),strList[0].trim(), strList[1].trim()
					});
				}
			}
		} catch (SQLException e) {
			Log.e("DD", "SQLException: " + e.getMessage());
		} catch (IOException e) {
			Log.e("DD", "SQLException: " + e.getMessage());
		} finally {
			if(cityBuffer != null){
				try {
					cityBuffer.close();
				} catch (IOException e) {
					Log.e("DD", "SQLException: " + e.getMessage());
				}
			}

			if(cityIs != null){
				try {
					cityIs.close();
				} catch (IOException e) {
					Log.e("DD", "SQLException: " + e.getMessage());
				}
			}
		}




	}
	// ----------------------鐪�----------------
	private void initProvinceTable(SQLiteDatabase db) {

		InputStream cityIs = null;
		BufferedReader cityBuffer = null;
		try {
			cityIs = mContext.getResources().openRawResource(R.raw.province);
			cityBuffer = new BufferedReader(new InputStreamReader(cityIs));
			String cityline = null;
			while((cityline = cityBuffer.readLine()) != null){
				if(!TextUtils.isEmpty(cityline)){

					String[] strList = cityline.split("\t");
					if (strList.length < 2) {
						continue;
					}

					db.execSQL("INSERT INTO province(proindex,proname) VALUES (?,?)",
							new Object[] {strList[0].trim(), strList[1].trim()});
				}
			}
		} catch (SQLException e) {
			Log.e("DD", "SQLException: " + e.getMessage());
		} catch (IOException e) {
			Log.e("DD", "SQLException: " + e.getMessage());
		} finally {
			if(cityBuffer != null){
				try {
					cityBuffer.close();
				} catch (IOException e) {
					Log.e("DD", "SQLException: " + e.getMessage());
				}
			}

			if(cityIs != null){
				try {
					cityIs.close();
				} catch (IOException e) {
					Log.e("DD", "SQLException: " + e.getMessage());
				}
			}
		}


	}

//	private void initCities3Table(SQLiteDatabase db) {
//		InputStream cityIs = null;
//		BufferedReader cityBuffer = null;
//		try {
//			cityIs = mContext.getResources().openRawResource(R.raw.cities3);
//			cityBuffer = new BufferedReader(new InputStreamReader(cityIs));
//			String cityline = null;
//			while((cityline = cityBuffer.readLine()) != null){
//				if(!TextUtils.isEmpty(cityline)){
//
//					String[] strList = cityline.split("\t");
//					if (strList.length < 6) {
//						continue;
//					}
//
//					db.execSQL("INSERT INTO citylistthress(proindex,proname,cityindex,cityname,twonindex,twonname) VALUES (?,?,?,?,?,?)",
//							new Object[] {strList[0].trim(), strList[1].trim(),
//							strList[2].trim(),strList[3].trim(),
//							strList[4].trim(),strList[5].trim()});
//				}
//			}
//		} catch (SQLException e) {
//			Log.e("DD", "SQLException: " + e.getMessage());
//		} catch (IOException e) {
//			Log.e("DD", "SQLException: " + e.getMessage());
//		} finally {
//			if(cityBuffer != null){
//				try {
//					cityBuffer.close();
//				} catch (IOException e) {
//					Log.e("DD", "SQLException: " + e.getMessage());
//				}
//			}
//
//			if(cityIs != null){
//				try {
//					cityIs.close();
//				} catch (IOException e) {
//					Log.e("DD", "SQLException: " + e.getMessage());
//				}
//			}
//		}
//	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	}

}
